package com.saifan.wyy_ov.ui.communitycenter;


import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.data.bean.MessageBean;
import com.saifan.wyy_ov.model.impl.GetData;
import com.saifan.wyy_ov.model.interfaces.ResponseData;
import com.saifan.wyy_ov.common.BaseFragment;
import com.saifan.wyy_ov.ui.view.RefreshLayout;
import com.saifan.wyy_ov.utils.CommonAdapter;
import com.saifan.wyy_ov.utils.StringUtil;
import com.saifan.wyy_ov.utils.ToastUtil;
import com.saifan.wyy_ov.utils.ViewHolderHelper;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @version V1.0.0
 * @author: lomasky
 * @date: 2015/6/9-21:19
 * @descrption 我的消息
 */
public class MessageFragment extends BaseFragment {


    private boolean isLoad;
    int page = 0;
    int type = 0;
    private RefreshLayout refreshLayout;
    private ListView listView;
    private List<MessageBean> beans = new ArrayList<>();
    private CommonAdapter<MessageBean> adapter;
    private View view;
    private GetData getData;
    private RelativeLayout loadlay;

    public MessageFragment() {
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        if (args != null) {
            type = args.getInt("type");
        }
        getData = new GetData();

    }

    public void query() {

        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("FKGX_ZJ", getRoomInfoBean().getFKGX_ZJ());
            jsonObject.put("Page", page);
            jsonObject.put("Type", type);
            if (beans.isEmpty()) {
                loadlay.setVisibility(View.VISIBLE);
            }
            getData.getDataJson(getActivity(), Api.Customer.MY_MESSAGE, jsonObject, "", new ResponseData() {
                @Override
                public void onSuccess(String json) {

                    List<MessageBean> list = new Gson().fromJson(json, new TypeToken<List<MessageBean>>() {
                    }.getType());

                    if (list != null && !list.isEmpty()) {
                        if (page == 0) {
                            beans = list;
                        } else {
                            beans.addAll(list);
                        }
                        adapter.mDatas = beans;
                        adapter.notifyDataSetChanged();
                        isLoad = true;

                    } else {

                        if (page > 0) {
                            page--;
                            ToastUtil.showToast(getActivity(),"暂无更多信息");
                        }else {

                            ToastUtil.showToast(getActivity(),"暂无相关信息");
                        }

                    }
                    loadlay.setVisibility(View.GONE);

                    refreshLayout.setRefreshing(false);
                    refreshLayout.setLoading(false);

                }

                @Override
                public void onFailed(String str) {
                    loadlay.setVisibility(View.GONE);
                    refreshLayout.setRefreshing(false);
                    refreshLayout.setLoading(false);
                    if (page > 0) page--;

                }
            });
        } catch (JSONException e) {
        }
    }

    @Override
    public void onResume() {
        super.onResume();

        if (!isLoad && isVisible()) {
            query();
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_message, container, false);
        this.refreshLayout = (RefreshLayout) view.findViewById(R.id.refreshLayout);
        this.listView = (ListView) view.findViewById(R.id.listView);
        this.loadlay = (RelativeLayout) view.findViewById(R.id.loading_lay);

        adapter = new CommonAdapter<MessageBean>(getActivity(), beans, R.layout.message_item) {
            @Override
            public void convert(ViewHolderHelper helper, MessageBean item) {

                helper.setText(R.id.WDXX_BT, StringUtil.returnShow(item.getWDXX_BT()));
                helper.setText(R.id.WDXX_LR, StringUtil.returnShow(item.getWDXX_LR()));
                helper.setText(R.id.WDXX_FSSJ, StringUtil.returnShow(item.getWDXX_FSSJ()));
            }
        };
        listView.setAdapter(adapter);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // TODO Auto-generated method stub
                page = 0;
                refreshLayout.setRefreshing(true);
                query();
            }
        });

        refreshLayout.setOnLoadListener(new RefreshLayout.OnLoadListener() {
            @Override
            public void onLoad() {
                // TODO Auto-generated method stub
                if (page > 0) {
                    refreshLayout.setLoading(true);
                }
                page++;
                query();
            }
        });


        return view;
    }

    public static MessageFragment newInstance(int type) {
        MessageFragment f = new MessageFragment();
        Bundle args = new Bundle();
        args.putInt("type", type);

        f.setArguments(args);
        return f;
    }

}
